/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean.inspector;

import com.jb.framework.datasource.TableModel;
import com.jb.web.bean.FormField;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.RendererImpl;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;


public class OptionPropertyEditor extends DefaultPropertyEditor {
    private String mOptions;

    public OptionPropertyEditor(String options) {
        super();
        mOptions = options;
    }

    public String getURL(FormField pItem) {
        return super.getWindowURL() + "?ds=" + mOptions + "&id=" + pItem.getId();
    }

    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        return getOptionHTML(pCtx, (FormField) itemToRender);
    }

    private StringBuffer getOptionHTML(WebContext pCtx, FormField pFormItem) {
        StringBuffer html = new StringBuffer("<SELECT name=\"").append(pFormItem.getClientId()).append("\"").append(com.jb.web.renderer.RendererImpl.addTagProperty(
                    "class", "propertyField", null));
        html.append(">");

        TableModel tableData = RendererImpl.optionData(pCtx, pFormItem, mOptions);

        if (tableData != null) {
            for (int i = 0; i < tableData.getMaxValue(); i++) {
                try {
                    Iterator row = tableData.getItem(i);
                    String dbName = (String) row.next().toString();
                    String dbValue = (String) row.next().toString();

                    if ((dbValue != null) &&
                            (dbValue.equals(com.jb.web.renderer.RendererImpl.evaluate(pCtx, pFormItem,
                                    pFormItem.getValue())))) {
                        html.append("<OPTION value=\"").append(dbValue).append("\" selected>").append(dbName).append("</OPTION>");
                    } else {
                        html.append("<OPTION value=\"").append(dbValue).append("\">").append(dbName).append("</OPTION>");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        html.append("</SELECT>");

        return html;
    }
}
